package com.test.schoolhelper.ui.activity;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.test.schoolhelper.R;
import com.test.schoolhelper.base.BaseActivity;
import com.test.schoolhelper.databinding.ActivityMainBinding;
import com.test.schoolhelper.ui.adapter.ViewPagerAdapter;
import com.test.schoolhelper.ui.fragment.NewsFragment;
import com.test.schoolhelper.ui.fragment.ToolsFragment;
import com.test.schoolhelper.ui.fragment.WeatherFragment;
import com.test.schoolhelper.ui.receiver.NetworkStateReceiver;

import java.util.ArrayList;

// TODO: 2024/5/29 主活动
public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private ArrayList<Fragment> mFragmentList = new ArrayList<>();
    private NetworkStateReceiver receiver;
    @Override
    protected void initData() {
        super.initData();
        registerReceiver();
        initFragmentList();
    }
    @Override
    protected void initClick() {
        super.initClick();
    }
    private void initFragmentList() {
        mFragmentList.add(new WeatherFragment());
        mFragmentList.add(new NewsFragment());
        mFragmentList.add(new ToolsFragment());

        getBinding().navigation.setItemIconTintList(null);
        getBinding().navigation.setItemIconSize(60);
        getBinding().navigation.setSelectedItemId(getBinding().navigation.getMenu().getItem(0).getItemId());

        initAdapter();
    }

    private void initAdapter() {
        ViewPagerAdapter fragmentAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        fragmentAdapter.setFragmentList(mFragmentList);
        getBinding().viewPager.setAdapter(fragmentAdapter);
        getBinding().viewPager.setOffscreenPageLimit(mFragmentList.size());//不预加载
        getBinding().viewPager.setCurrentItem(0);

        getBinding().viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int position) {
                getBinding().navigation.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });

        getBinding().navigation.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_weather:
                    getBinding().viewPager.setCurrentItem(0);
                    break;
                case R.id.navigation_news:
                    getBinding().viewPager.setCurrentItem(1);
                    break;
                case R.id.navigation_tools:
                    getBinding().viewPager.setCurrentItem(2);
                    break;
            }
            return false;
        });
    }


    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected ActivityMainBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityMainBinding.inflate(layoutInflater);
    }

    private void registerReceiver() {
        receiver = new NetworkStateReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, filter);
    }

    private void unregisterReceiver() {
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver();
    }
}